package com.example.ccj.b.c.form;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.TransverseLoadImageControl;
import com.example.ccj.u.StringUtils;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

/**
 * 显示数据字段view
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/7/1 13:28
 */
public class ShowDataFieldView extends LinearLayout {

    TextView fieldName,fieldContent;
    ImageView jump;
    TransverseLoadImageControl imageControl;
    DataConfig dataConfig;


    public ShowDataFieldView(Context context) {
        super(context);
        initView();
    }

    public ShowDataFieldView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView(){
        LayoutInflater.from(getContext()).inflate(R.layout.custom_show_data_field, this);
        fieldName = findViewById(R.id.custom_show_data_field_name);
        fieldContent = findViewById(R.id.custom_show_data_field_content);
        jump = findViewById(R.id.custom_show_data_field_jump);
        imageControl = findViewById(R.id.custom_show_data_field_img);
        imageControl.setCanDelete(false);
    }

    public DataConfig getDataConfig() {
        return dataConfig;
    }

    public void setDataConfig(DataConfig dataConfig) {
        this.dataConfig = dataConfig;
        jxConfig(dataConfig);
    }

    String contentValue;

    //解析配置文件
    private void jxConfig(DataConfig dataConfig){
        //字段名
        fieldName.post(() -> fieldName.setText(dataConfig.getFieldName()));
        //字段值
        if (dataConfig.getFormDataFormatter() != null) {
            contentValue = dataConfig.getFormDataFormatter().result(dataConfig.dataName, this);
        }
        if (contentValue == null && !StringUtils.isEmpty(dataConfig.getText())) {
            contentValue = dataConfig.getText().toString();
        }
        fieldContent.post(() -> fieldContent.setText(contentValue));
        dataConfig.getFormDataFormatter().handlerData(dataConfig.dataName, this);
        //跳转处理
        if (dataConfig.type == MFormDataField.JUMP) {
            jump.post(() -> jump.setVisibility(View.VISIBLE));
            jump.post(() -> jump.setOnClickListener(v -> {
                if (dataConfig.getFormDataFormatter() != null) dataConfig.getFormDataFormatter().jump(dataConfig.dataName,ShowDataFieldView.this);
            }));
        }

        //图片处理
        if (dataConfig.type == MFormDataField.IMG) {
            imageControl.post(() -> imageControl.setVisibility(View.VISIBLE));
            List<LocalMedia> localMediaList = new ArrayList<>();
            if (dataConfig.getText() != null) {
                fieldContent.post(() -> fieldContent.setVisibility(View.GONE));
                String[] imgArray = dataConfig.getText().toString().split(",");
                if (imgArray.length > 0) {
                    for (String s : imgArray) {
                        if (!StringUtils.isEmpty(s)) {
                            LocalMedia localMedia = LocalMedia.generateHttpAsLocalMedia(s);
                            localMediaList.add(localMedia);
                        }
                    }
                    imageControl.post(() -> imageControl.setPictureList(localMediaList));
                }
            }
        }
    }

    public String getContentValue() {
        return contentValue;
    }

    public void setContentValue(String contentValue) {
        this.contentValue = contentValue;
        fieldContent.post(() -> fieldContent.setText(contentValue));
    }
}
